package me.yricky.alia.ui.component

import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Folder
import androidx.compose.material.icons.rounded.InsertDriveFile
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import java.io.File

/**
 * @author Yricky
 * @date 2022/2/5
 */

@Composable
fun ListWithScrollBar(
    modifier: Modifier = Modifier,
    list:LazyListScope.()->Unit
){
    Box(modifier){
        val state = rememberLazyListState()
        LazyColumn(
            Modifier.fillMaxSize(),
            state = state
        ) {
            list()
        }
        VerticalScrollbar(
            modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight(),
            adapter = rememberScrollbarAdapter(state)
        )
    }
}

@Composable
fun FileList(
    modifier: Modifier = Modifier,
    files:List<String> = emptyList(),
    onClick:(File)->Unit = {}
){
    ListWithScrollBar(modifier) {
        items(files){
            val f = File(it)
            Row(Modifier
                .fillMaxWidth()
                .clickable {
                    onClick(f)
                }
                .padding(0.dp,4.dp)
            ) {
                Icon(
                    if(f.isDirectory){ Icons.Rounded.Folder }
                    else{ Icons.Rounded.InsertDriveFile },
                    "icon",
                    modifier = Modifier.size(24.dp))
                Text(f.name)
            }
        }
    }
}